package com.wxgo.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.wxgo.common.model.Module;
import com.wxgo.common.model.User;
import com.wxgo.factory.DaoFactory;

public class AdminController extends Controller {

	public void login() {
		render("/login.html");
	}

	public void doLogin() {
		String account = getPara("uname");
		String password = getPara("password");
		User user = DaoFactory.userDao().findByNameAndPwd(account, password);

		boolean code = false;
		String msg = "登录成功了";
		if (validateCaptcha("code")) {
			if (user != null && user.getAccount() != null) {
				setSessionAttr("uid", user.getId());
				setSessionAttr("rid", user.getRid());
				setSessionAttr("nick", user.getNick());
				setSessionAttr("account", user.getAccount());
				code = true;
			} else {
				msg = "账户或密码错误!";
			}
		} else {
			msg = "验证码错误";
		}
		renderJson(new Ret().set("code", code).set("msg", msg));
	}

	public void home() {
		List<Module> parentList = DaoFactory.moduleDao().findAllModule();
		Map<String, List<Module>> moduleMap = new HashMap<String, List<Module>>();
		List<Module> temp = null;
		for (Module m : parentList) {
			temp = new ArrayList<Module>();
			if (m.getPmid() == 0) {
				temp.add(m);
				moduleMap.put(m.getId().toString(), temp);
			} else {
				temp = moduleMap.get(m.getPmid().toString());
				temp.add(m);
				moduleMap.put(m.getPmid().toString(), temp);
			}
		}
		setAttr("moduleMap", moduleMap);
		render("/admin/index1.html");
	}

	public void captcha() {
		renderCaptcha();
	}
}
